package es.udc.pfc.davidparedes.model.universe;

import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;

import org.hibernate.annotations.Where;

import es.udc.pfc.davidparedes.model.universemanager.UniverseManager;
import es.udc.pfc.davidparedes.model.vendor.Vendor;
import es.udc.pfc.davidparedes.model.store.Store;

@Entity
public class Universe {
	
	private long universeId;
	private String name;
	private UniverseManager manager;
	private Store store;
	private Calendar deletionDate;
	private long version;
	
	private Set<Vendor> vendors = new HashSet<Vendor>();
	
	public Universe() {
	}
	
	public Universe(String name, Store store) {
		this.name = name;
		this.store = store;
		store.addUniverse(this);
	}
	
	@Column (name = "id")
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getUniverseId() {
		return universeId;
	}
	
	public void setUniverseId(long universeId) {
		this.universeId = universeId;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	@OneToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="manager")
	public UniverseManager getManager() {
		return manager;
	}
	
	public void setManager(UniverseManager manager) {
		this.manager = manager;
	}
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="store")
	public Store getStore() {
		return store;
	}
	
	public void setStore(Store store) {
		this.store = store;
	}

	@OneToMany(mappedBy = "universe")
	@Where(clause="type='vendor'")
    public Set<Vendor> getVendors() {
        return vendors;
    }
	
    public void setVendors(Set<Vendor> vendors) {
    	this.vendors = vendors;
    }
    
    public void addVendor(Vendor vendor) {
    	vendors.add(vendor);
    	vendor.setUniverse(this);
    }
    
    public void removeVendor(Vendor vendor) {
    	vendors.remove(vendor);
    	vendor.setUniverse(null);
    }
	
    @Temporal(TemporalType.TIMESTAMP)
	public Calendar getDeletionDate() {
		return deletionDate;
	}
	
	public void setDeletionDate(Calendar deletionDate) {
		this.deletionDate = deletionDate;
	}
	
	@Version
	public long getVersion() {
		return version;
	}
	
	public void setVersion(long version) {
		this.version = version;
	}
	
	@Override
	public String toString() {
		return (name);
	}

}
